package com.quanxiaoha.weblog.web.controller;

import com.quanxiaoha.weblog.common.aspect.ApiOperationLog;
import com.quanxiaoha.weblog.common.utils.Response;
import com.quanxiaoha.weblog.web.model.vo.ai.AiChatReqVO;
import com.quanxiaoha.weblog.web.model.vo.ai.CreateNewConversationReqVO;
import com.quanxiaoha.weblog.web.model.vo.ai.DeleteAiConversationReqVO;
import com.quanxiaoha.weblog.web.model.vo.ai.GetAiChatHistoryReqVO;
import com.quanxiaoha.weblog.web.model.vo.ai.GetConversationListReqVO;
import com.quanxiaoha.weblog.web.model.vo.ai.TextSelectionAiReqVO;
import com.quanxiaoha.weblog.web.service.AiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import org.springframework.http.MediaType;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2024-01-15
 * @description: AI助手
 **/
@RestController
@RequestMapping("/ai")
@Api(tags = "AI助手")
public class AiController {

    @Autowired
    private AiService aiService;

    @PostMapping("/chat")
    @ApiOperation(value = "AI助手聊天")
    @ApiOperationLog(description = "AI助手聊天")
    public Response chat(@RequestBody @Validated AiChatReqVO aiChatReqVO) {
        return aiService.chat(aiChatReqVO);
    }

    @PostMapping("/conversation/delete")
    @ApiOperation(value = "删除AI对话")
    @ApiOperationLog(description = "删除AI对话")
    public Response deleteConversation(@RequestBody @Validated DeleteAiConversationReqVO deleteAiConversationReqVO) {
        return aiService.deleteConversation(deleteAiConversationReqVO);
    }

    @PostMapping("/conversation/history")
    @ApiOperation(value = "获取AI聊天历史记录")
    @ApiOperationLog(description = "获取AI聊天历史记录")
    public Response getChatHistory(@RequestBody @Validated GetAiChatHistoryReqVO getAiChatHistoryReqVO) {
        return aiService.getChatHistory(getAiChatHistoryReqVO);
    }

    @PostMapping("/conversation/new")
    @ApiOperation(value = "创建新对话")
    @ApiOperationLog(description = "创建新对话")
    public Response createNewConversation(@RequestBody @Validated CreateNewConversationReqVO createNewConversationReqVO) {
        return aiService.createNewConversation(createNewConversationReqVO);
    }

    @PostMapping("/conversation/list")
    @ApiOperation(value = "获取对话列表")
    @ApiOperationLog(description = "获取对话列表")
    public Response getConversationList(@RequestBody @Validated GetConversationListReqVO getConversationListReqVO) {
        return aiService.getConversationList(getConversationListReqVO);
    }

    @PostMapping(value = "/text-selection")
    @ApiOperation(value = "文章划词AI交互")
    @ApiOperationLog(description = "文章划词AI交互")
    public Response textSelectionChat(@RequestBody @Validated TextSelectionAiReqVO textSelectionAiReqVO) {
        return aiService.textSelectionChat(textSelectionAiReqVO);
    }

    @PostMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @ApiOperation(value = "AI助手流式聊天")
    @ApiOperationLog(description = "AI助手流式聊天")
    public ResponseEntity<StreamingResponseBody> streamChat(@RequestBody @Validated AiChatReqVO aiChatReqVO) {
        return aiService.streamChat(aiChatReqVO);
    }
}